<?php

/*
	info: 积分兑换
	date: 2011-12-19
*/

if (!defined('ADMIN_WEBINROOT'))
{
	header("HTTP/1.0 404 Not Found");
	exit();
}

$webglobal['file_integral'] = array();
$webglobal['file_integral']['imagepath'] = '/upfiles/integral/'.global_FormatTime(0, 'Ym');
$webglobal['file_integral']['imagesetting'] = array('type'=>array('gif', 'jpg', 'jpeg', 'png', 'bmp'), 'size'=>2097152);

//商铺编辑
if ($webglobal['mode']=='integraledit')
{
	$webrequest['editid'] = isset($webrequest['editid']) ? intval($webrequest['editid']) : 0;
	if ($webrequest['editid'] <= 0) {
		admin_ShowMessage('不存在该兑换商品。', '', 'BACK');
	}
	$webglobal['file_integral']['editinfo'] = $db->GetOne("SELECT * FROM {$webconfig['dbpre']}integral_goods WHERE id='{$webrequest['editid']}'");
	if (!$webglobal['file_integral']['editinfo']) {
		admin_ShowMessage('不存在该兑换商品。', '', 'BACK');
	}

	if (global_CheckSubmit('adminsubmit'))
	{
		include WEBFILEPATH.'/include/upload.class.php';
		global_CheckPost(
				array(
					array('name,','str','','标题不能为空',1),
					array('score,','int','','兑换所需积分格式必须为整数',0,0),
					array('status,','int','','',0,0),
					array('image_del','int','','',0,0),
					array('description,','','','描述不能为空',1),
			));

		if (!empty($webglobal['CPM'])) {
			admin_ShowMessage($webglobal['CPM'], '', 'BACK');
		} else {

			include WEBFILEPATH.'/include/upload.class.php';

			$tempmoreshowinfo = $tempimageurl = '';
			$tempimageupload = new Upload('image', $webglobal['file_integral']['imagesetting']);
			if ($tempimageupload->CheckFile()) {
				$tempimageupload->SaveFile(WEBFILEPATH.$webglobal['file_integral']['imagepath']);
				$tempimageurl = $webglobal['file_integral']['imagepath'].'/'.$tempimageupload->fileList[0]['name'];

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/' . $tempimageupload->fileList[0]['name'];

				include WEBFILEPATH.'/include/image.class.php';
				$objImg = new Image();

				//Big Image:205 133
				$bigImage = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/big_' . $tempimageupload->fileList[0]['name'];
				$middleImage = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/middle_' . $tempimageupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/small_' . $tempimageupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 205 , 133 , $bigImage, 5, FALSE);
				$objImg->OnceResize($imageFilePath , 707 , 325 , $middleImage, 123, FALSE);
				$objImg->OnceResize($imageFilePath , 84 , 84 , $smallImage, 5, FALSE);

			} elseif (!$tempimageupload->isNone) {
				$tempmoreshowinfo .= '<br /><span class="dark">上传的logo文件不符合条件已忽略。</span>';
			}

			if ($webglobal['CPP']['image_del']>0 || $tempimageurl!='')
			{
				if ($webglobal['file_integral']['editinfo']['image'] != '')
				{
					@unlink(WEBFILEPATH.$webglobal['file_integral']['editinfo']['image']);
				}
				$webglobal['CPD']['image'] = $tempimageurl;
			}

			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=integral&{$webrequest['gourl']}");

			$db->Update('integral_goods', '', "id='{$webrequest['editid']}'");

			admin_AddLog("修改兑换商品|id: {$webrequest['editid']}");
			admin_ShowMessage("兑换商品修改成功。{$tempmoreshowinfo}", ADMIN_WEBURLPATH."/index.php?mode=integral&{$webrequest['gourl']}");
		}
	} else {
		$webglobal['file_integral']['gourlencode'] = urlencode($webrequest['gourl']);
		$webglobal['file_integral']['editinfo']['addtime_show'] = !empty($webglobal['file_integral']['editinfo']['addtime']) ? global_FormatTime($webglobal['file_integral']['editinfo']['addtime']) : '';
		admin_StartFromForm();
	}
}
//批量更改
elseif ($webglobal['mode']=='integralup')
{
	if (!global_CheckCome()) {
		admin_ShowMessage('参数错误。', '', 'BACK');
	}

	global_CheckPost(
				array(
					array('delid[]','int','',''),
					array('auditid[]','int','',''),
					array('access','str','','',0,'update'),
			));

	$tempauditstr = '';
	if ($webglobal['CPP']['access'] == 'update') {
		if (!empty($webglobal['CPP']['delid'])) {
			$tempauditstr = implode(',', $webglobal['CPP']['delid']);
			$db->Update('integral_goods', "status='1'", 'id IN ('.$db->BuildInSql($webglobal['CPP']['delid']).')');
		}
	}

	if ($webglobal['CPP']['access'] == 'delete') {
		if (!empty($webglobal['CPP']['delid'])) {
			$tempdelstr = implode(',', $webglobal['CPP']['delid']);
			$db->Update('integral_goods', "status='-1'", 'id IN ('.$db->BuildInSql($webglobal['CPP']['delid']).')');
		}
	}
	
	if ($webglobal['CPP']['access'] == 'down') {
		if (!empty($webglobal['CPP']['delid'])) {
			$tempdelstr = implode(',', $webglobal['CPP']['delid']);
			$db->Update('integral_goods', "status='0'", 'id IN ('.$db->BuildInSql($webglobal['CPP']['delid']).')');
		}
	}

	$templogmsg = '';
	if (!empty($tempauditstr))
	{
		$templogmsg .= (empty($templogmsg) ? '' : '; ')."audit: {$tempauditstr}";
	}
	if (!empty($tempdelstr))
	{
		$templogmsg .= (empty($templogmsg) ? '' : '; ')."del: {$tempdelstr}";
	}
	if (empty($templogmsg))
	{
		admin_ShowMessage("没有任何条目被修改，系统自动返回。", ADMIN_WEBURLPATH."/index.php?mode=integral&{$webrequest['gourl']}");
	}

	admin_AddLog("批量更改积分兑换商品|{$templogmsg}");
	admin_ShowMessage('批量更改积分兑换商品属性成功。', ADMIN_WEBURLPATH."/index.php?mode=integral&{$webrequest['gourl']}");

//添加积分兑换商品
} elseif ($webglobal['mode']=='integraladd') {
	if (global_CheckSubmit('adminsubmit')) {

		global_CheckPost(
				array(
					array('name,','str','','标题不能为空',1),
					array('score,','int','','兑换所需积分格式必须为整数',0,0),
					array('status,','int','','',0,0),
					array('description,','','','描述不能为空',1),
			));

		if (!empty($webglobal['CPM']))
		{
			admin_ShowMessage($webglobal['CPM'], '', 'BACK');
		}
		else
		{
			admin_CheckFromForm(ADMIN_WEBURLPATH."/index.php?mode=integral");
			
			include WEBFILEPATH.'/include/upload.class.php';
			$tempmoreshowinfo = $tempimageurl = '';
			$tempimageupload = new Upload('image', $webglobal['file_integral']['imagesetting']);

			if ($tempimageupload->CheckFile()) {
				$tempimageupload->SaveFile(WEBFILEPATH.$webglobal['file_integral']['imagepath']);
				$tempimageurl = $webglobal['file_integral']['imagepath'].'/'.$tempimageupload->fileList[0]['name'];

				//生成缩略图
				$imageFilePath = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/' . $tempimageupload->fileList[0]['name'];

				include WEBFILEPATH.'/include/image.class.php';
				$objImg = new Image();

				//Big Image:205 133
				$bigImage = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/big_' . $tempimageupload->fileList[0]['name'];
				$middleImage = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/middle_' . $tempimageupload->fileList[0]['name'];
				$smallImage = WEBFILEPATH.$webglobal['file_integral']['imagepath'] . '/small_' . $tempimageupload->fileList[0]['name'];

				$objImg->OnceResize($imageFilePath , 205 , 133 , $bigImage, 123, FALSE);
				$objImg->OnceResize($imageFilePath , 707 , 325 , $middleImage, 123, FALSE);
				$objImg->OnceResize($imageFilePath , 84 , 84 , $smallImage, 5, FALSE);

			} elseif (!$tempimageupload->isNone) {
				$tempmoreshowinfo .= '<br /><span class="dark">上传的图片文件不符合条件已忽略。</span>';
			}

			if ($tempimageurl!='') {
				$webglobal['CPD']['image'] = $tempimageurl;
			}

			$webglobal['CPD']['addtime'] = $webserver['timestamp'];
			$webglobal['CPD']['admin_user_id'] = $user['adminuid'];

			$tempnewsid = $db->Insert('integral_goods', '', TRUE);

			admin_AddLog("添加积分兑换商品|id: {$tempnewsid}; title: {$webglobal['CPP']['name']}");
			admin_ShowMessage('积分兑换商品添加成功。', ADMIN_WEBURLPATH."/index.php?mode=integral");
		}
	} else {
		admin_StartFromForm();
	}


} elseif ($webglobal['mode']=='integralorderup') {
	if (!global_CheckCome()) {
		admin_ShowMessage('参数错误。', '', 'BACK');
	}

	global_CheckPost(
				array(
					array('auditid[]','int','',''),
			));

	$tempauditstr = '';
	if (!empty($webglobal['CPP']['auditid'])) {
		if($webrequest['formaction']=='check'){
			$status = 1;
		}else if($webrequest['formaction']=='sendgoods'){
			$status = 2;
		}else if($webrequest['formaction']=='retriever'){
			$status = 3;
		}else{
			$status = '-1';
		}
		$tempauditstr = implode(',', $webglobal['CPP']['auditid']);
		$db->Update('integral_order', "status='{$status}'", 'id IN ('.$db->BuildInSql($webglobal['CPP']['auditid']).')');
	}

	$templogmsg = '';
	if (!empty($tempauditstr))
	{
		$templogmsg .= (empty($templogmsg) ? '' : '; ')."audit: {$tempauditstr}";
	}

	if (empty($templogmsg))
	{
		admin_ShowMessage("没有任何条目被修改，系统自动返回。", ADMIN_WEBURLPATH."/index.php?mode=integralorder&{$webrequest['gourl']}");
	}

	admin_AddLog("批量更改积分兑换订单|{$templogmsg}");
	admin_ShowMessage('批量更改积分兑换订属性成功。', ADMIN_WEBURLPATH."/index.php?mode=integralorder&{$webrequest['gourl']}");
//积分兑换订单列表
} elseif ($webglobal['mode']=='integralorder') {
	global_CheckPost(
				array(
					array('order','str','','',0,''),
					array('searchkey','str','','',0,''),
					array('status','int','','',0,999)
			));

	$tempsearchsql = array();
	$webglobal['file_integral']['gourl'] = "page={$webrequest['page']}";
	$webglobal['file_integral']['searchurl'] = "";
	if ($webglobal['CPP']['status'] != 999)
	{
		$tempsearchsql[] = "o.status='{$webglobal['CPP']['status']}'";
		$webglobal['file_integral']['gourl'] .= "&status={$webglobal['CPP']['status']}";
		$webglobal['file_integral']['searchurl'] .= "&status={$webglobal['CPP']['status']}";
	}
	if ($webglobal['CPP']['searchkey'] != '')
	{
		$tempsearchsql[] = $db->BuildSearchSql($webglobal['CPP']['searchkey'], array('g.name','g.description', 'u.username', 'u.email', 'u.truename', 'u.nickname'));
		$webglobal['file_integral']['gourl'] .= "&searchkey=".urlencode($webglobal['CPP']['searchkey']);
		$webglobal['file_integral']['searchurl'] .= "&searchkey=".urlencode($webglobal['CPP']['searchkey']);
	}
	if ($webglobal['CPP']['order'] != '')
	{
		$webglobal['file_integral']['gourl'] .= "&order={$webglobal['CPP']['order']}";
	}
	$tempsearchsql = empty($tempsearchsql) ? '' : ' WHERE '.implode(' AND ', $tempsearchsql);
	$webglobal['file_integral']['gourlencode'] = urlencode($webglobal['file_integral']['gourl']);

	$webglobal['file_integral']['perpage'] = 1;
	$webglobal['file_integral']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}integral_order o LEFT JOIN {$webconfig['dbpre']}integral_goods g ON o.integral_goods_id = g.id LEFT JOIN {$webconfig['dbpre']}user u ON o.user_id = u.id {$tempsearchsql}");

	if ($webglobal['file_integral']['datanum'] > 0)
	{
		$webglobal['page'] = global_Page($webglobal['file_integral']['datanum'], $webglobal['file_integral']['perpage'], $webrequest['page'], ADMIN_WEBURLPATH."/index.php?mode=integralorder&{$webglobal['file_integral']['searchurl']}&page=@@");
		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_integral']['perpage'];
		switch ($webglobal['CPP']['order'])
		{
			case 'id':
				$temporderstr = "id DESC";
				break;
			case 'addtime':
				$temporderstr = "addtime DESC,id DESC";
				break;
			default:
				$temporderstr = "id DESC";
		}
		$query = $db->Query("SELECT o.*, g.id as g_id, g.name as g_name, g.status as g_status, u.id as u_id, u.email as u_email, u.auth_tel as u_auth_tel, u.auth_indentity as u_auth_indentity, u.status as u_status FROM {$webconfig['dbpre']}integral_order o LEFT JOIN {$webconfig['dbpre']}integral_goods g ON o.integral_goods_id = g.id LEFT JOIN {$webconfig['dbpre']}user u ON o.user_id = u.id  {$tempsearchsql}  LIMIT $tempstartnum, {$webglobal['file_integral']['perpage']}");
		$temptr = 1;
		$webglobal['file_integral']['dataarray'] = array();
		$tempCompanyId = array();

		while ($tempdata = $db->FetchArray($query)) {
			$tempdata['tdclass'] = (($temptr++)%2==0) ? ' class="evenrow"' : '';
			$tempdata['addtime_show'] = !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
			$tempdata['user_status_show'] = isset($tempdata['u_id']) && $tempdata['u_id'] ? showStatus($tempdata['u_status']) : array();
			$tempdata['total_score'] = $tempdata['unit_score'] * $tempdata['quantity'];
			$webglobal['file_integral']['dataarray'][$tempdata['id']] = $tempdata;
		}
	}
//列表
} elseif ($webglobal['mode']=='integral') {
	global_CheckPost(
				array(
					array('order','str','','',0,''),
					array('searchkey','str','','',0,''),
					array('status','int','','',0,999)
			));

	$tempsearchsql = array();
	$webglobal['file_integral']['gourl'] = "page={$webrequest['page']}";
	$webglobal['file_integral']['searchurl'] = "";
	if ($webglobal['CPP']['status'] != 999)
	{
		$tempsearchsql[] = "status='{$webglobal['CPP']['status']}'";
		$webglobal['file_integral']['gourl'] .= "&status={$webglobal['CPP']['status']}";
		$webglobal['file_integral']['searchurl'] .= "&status={$webglobal['CPP']['status']}";
	}
	if ($webglobal['CPP']['searchkey'] != '')
	{
		$tempsearchsql[] = $db->BuildSearchSql($webglobal['CPP']['searchkey'], array('name','description'));
		$webglobal['file_integral']['gourl'] .= "&searchkey=".urlencode($webglobal['CPP']['searchkey']);
		$webglobal['file_integral']['searchurl'] .= "&searchkey=".urlencode($webglobal['CPP']['searchkey']);
	}
	if ($webglobal['CPP']['order'] != '')
	{
		$webglobal['file_integral']['gourl'] .= "&order={$webglobal['CPP']['order']}";
	}
	$tempsearchsql = empty($tempsearchsql) ? '' : ' WHERE '.implode(' AND ', $tempsearchsql);
	$webglobal['file_integral']['gourlencode'] = urlencode($webglobal['file_integral']['gourl']);

	$webglobal['file_integral']['perpage'] = 10;
	$webglobal['file_integral']['datanum'] = $db->GetNum("SELECT COUNT(*) FROM {$webconfig['dbpre']}integral_goods{$tempsearchsql}");
	if ($webglobal['file_integral']['datanum'] > 0)
	{
		$webglobal['page'] = global_Page($webglobal['file_integral']['datanum'], $webglobal['file_integral']['perpage'], $webrequest['page'], ADMIN_WEBURLPATH."/index.php?mode=integral&order={$webglobal['CPP']['order']}{$webglobal['file_integral']['searchurl']}&page=@@");
		$tempstartnum = ($webrequest['page']-1)*$webglobal['file_integral']['perpage'];
		switch ($webglobal['CPP']['order'])
		{
			case 'id':
				$temporderstr = "id DESC";
				break;
			case 'pubtime':
				$temporderstr = "addtime DESC,id DESC";
				break;
			default:
				$temporderstr = "id DESC";
		}
		$query = $db->Query("SELECT * FROM {$webconfig['dbpre']}integral_goods{$tempsearchsql} ORDER BY {$temporderstr} LIMIT $tempstartnum, {$webglobal['file_integral']['perpage']}");
		$temptr = 1;
		$webglobal['file_integral']['dataarray'] = array();
		$tempCompanyId = array();

		while ($tempdata = $db->FetchArray($query)) {
			$tempdata['tdclass'] = (($temptr++)%2==0) ? ' class="evenrow"' : '';
			$tempdata['addtime_show'] = !empty($tempdata['addtime']) ? global_FormatTime($tempdata['addtime']) : '';
			$webglobal['file_integral']['dataarray'][$tempdata['id']] = $tempdata;
		}
	}
}

function showStatus($type) {
	$status = array(
		-1 => array('m-error', '禁止登录'),
		0 => array('m-warn', '未认证'),
		1=> array('m-success', '认证通过')
	);

	return isset($status[$type]) ? $status[$type] : '';
}

template_Display('integral', 1);
